<?php

use app\models\Isp;
use app\models\PackageIsp;
use app\models\Cardtype;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;
use dosamigos\datepicker\DatePicker;
use app\models\UserConfig;

/**
 * @var $this yii\web\View
 * @var $card app\models\Card
 */

$this->title = '新增入库单';
$this->params['breadcrumbs'][] = '入库管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $form = ActiveForm::begin();?>
<div class="form-group">
    <label for="pur_time">采购时间</label>
    <?php echo DatePicker::widget([ 
        'name' => 'Storage[pur_time]', 
        'options' => ['placeholder' => ''], 
        'attribute' => 'pur_time',
        'template' => '{addon}{input}',
        'value' => date('Y-m-d'),
        'clientOptions' => [ 
            'autoclose' => true, 
            'format' => 'yyyy-mm-dd',
            'todayHighlight' => true, 
        ] 
    ]); ?>
    
</div>

<div class="row">
	<div class="col-md-3"><?php echo $form->field($card, 'pur_name')->textInput(['autocomplete' => 'off']);?></div>
	<div class="col-md-3"><?php echo $form->field($card, 'isp_id')->dropDownList(ArrayHelper::map(ISP::find()->all(), 'id', 'name'), ['prompt' => '选择运营商']);?></div>
	<div class="col-md-3"><?php echo $form->field($card, 'package_id')->dropDownList(ArrayHelper::map(PackageIsp::find()->all(), 'id', 'name'), ['prompt' => '选择套餐']);?></div>
	<div class="col-md-3"><?php echo $form->field($card, 'number')->textInput(['autocomplete' => 'off']);?></div>
</div>
<div class="row">
	<div class="col-md-3"><?php echo $form->field($card, 'simtype')->dropDownList(ArrayHelper::map(Cardtype::find()->all(), 'id', 'name'), ['prompt' => '选择卡类型']);?></div>
    <div class="col-md-3"><?php echo $form->field($card,'price')->textInput(['readonly'=>'true']);?></div>
	<div class="col-md-3"><?php echo $form->field($card, 'total_price')->textInput(['readonly' => 'true']);?></div>
	<div class="col-md-3"><?php echo $form->field($card, 'order')->textInput(['placeholder' => '不填写则会自动生成默认值。']);?></div>
</div>
<div class="form-group">
    <label for="silent_over">沉默期到期时间</label>
    <?php echo DatePicker::widget([ 
        'name' => 'Storage[silent_over]', 
        'options' => ['placeholder' => ''], 
        'attribute' => 'date',
        'template' => '{addon}{input}',
        'value' => date('Y-m-d'),
        'clientOptions' => [ 
            'autoclose' => true, 
            'format' => 'yyyy-mm-dd',
            'todayHighlight' => true, 
        ] 
    ]); ?>
    
</div>
<div class="row">
	<div class="col-md-3"><?php echo $form->field($card, 'silent_time')->textInput(['autocomplete' => 'off']);?></div>

</div>

<?php echo $form->field($card, 'payment_type')->radioList(['1'=>'预付费','2'=>'后付费'])->label('付费方式') ?>
<?php echo $form->field($card, 'pre_time')->textInput(['placeholder' => '预付费用户填写预存时长']);?>
<div class="form-group">
    <div class="col-lg-offset-1 col-lg-11">
        <button type="button" class="btn btn-default" onclick="window.history.go(-1);"><i class="ace-icon fa fa-arrow-left bigger-110"></i>返回</button>
        <button class="btn btn-primary"><i class="ace-icon fa fa-check bigger-110"></i>保存</button>
        <button type="reset" class="btn btn-warning"><i class="ace-icon fa fa-undo bigger-110"></i>重置</button>
    </div>
</div>
<?php $form->end();?>

<script>
    function pageInit() {

        $('[name="Storage[package_id]"]').change(function () {
            var id = $(this).val();
            $.ajax({
                url:"packageprice?id="+id,    //请求的url地址
                dataType:"json",   //返回格式为json
                async:true,
                type:"get",
                success:function(req){
                    if(req.code==1){
                        var number = $('[name="Storage[number]"]').val();
                        if(number==''){
                            $('[name="Storage[total_price]"]').val(req.data.price);
                        }else{
                            $('[name="Storage[total_price]"]').val(accMul(req.data.price,number));
                        }
                        $('[name="Storage[price]"]').attr("value",req.data.price);
                    }
                }
            });

        })

        
        $('[name="Storage[price]"]').change(function(){
            var price = $(this).val();
            var number = $('[name="Storage[number]"]').val();
            if(number==''){
                $('[name="Storage[total_price]"]').val(price);
            }else{
                $('[name="Storage[total_price]"]').val(accMul(price,number));
            }
        });
        $('[name="Storage[number]"]').keyup(function(){
            var number = $(this).val();
            var price = $('[name="Storage[price]"]').val();
            if(price==''){
                $('[name="Storage[total_price]"]').val(price);
            }else{
                $('[name="Storage[total_price]"]').val(accMul(price,number));
            }
        });
        //处理js乘法精度方法
        function accMul(arg1,arg2){
            var m=0,s1=arg1.toString(),s2=arg2.toString();
            try{m+=s1.split(".")[1].length}catch(e){}
            try{m+=s2.split(".")[1].length}catch(e){}
            return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
        }

    }

</script>